package com.hbpa.pan.server.modules.user.controller;

import cn.hutool.core.util.ObjectUtil;
import com.hbpa.pan.core.response.ResponseCode;
import com.hbpa.pan.core.response.ResponseEntity;
import com.hbpa.pan.core.utils.IdUtil;
import com.hbpa.pan.server.common.annotation.LoginIgnore;
import com.hbpa.pan.server.modules.user.context.*;
import com.hbpa.pan.server.modules.user.converter.UserConverter;
import com.hbpa.pan.server.modules.user.po.*;
import com.hbpa.pan.server.modules.user.service.IUserService;
import com.hbpa.pan.server.modules.user.utils.UserIdUtil;
import com.hbpa.pan.server.modules.user.vo.UserInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author: BlcakTiger
 * @date: 2023/10/22 10:27
 * @contact: hsf6661@163.com
 * @description:
 */
@RestController
@RequestMapping(value = "/user")
@Api(tags = "用户模块")
@CrossOrigin
public class UserController {

    @Autowired
    private IUserService iUserService;
    @Autowired
    private UserConverter userConverter;

    @ApiOperation(
            value = "用户注册接口",
            notes = "该接口提供了用户注册的功能,实现了幂等性,可以放心多并发使用",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @LoginIgnore
    @PostMapping("/register")
    public ResponseEntity register(@Validated @RequestBody UserRegisterPO userRegisterPO){
        //实体转化
        UserRegisterContext userRegisterContext = userConverter.userRegisterPO2UserRegisterContext(userRegisterPO);
        //注册用户
        Long userId = iUserService.register(userRegisterContext);
        return ResponseEntity.data(IdUtil.encrypt(userId));
    }

    @ApiOperation(
            value = "用户登录接口",
            notes = "该接口提供了用户登录的功能,成功登录之后，会返回有效性的accessToken供后续服务使用",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @LoginIgnore
    @PostMapping("/login")
    public ResponseEntity<String> login(@Validated @RequestBody UserLoginPO userLoginPO){
        UserLoginContext userLoginContext = userConverter.userLoginPO2UserLoginContext(userLoginPO);
        String accessToken = iUserService.login(userLoginContext);

        return ResponseEntity.success(200,"登陆成功",accessToken);
    }


    @ApiOperation(
            value = "用户注销接口",
            notes = "该接口提供了用户注销的功能",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @PostMapping("/logout")
    public ResponseEntity<String> logout(){
        iUserService.exit(UserIdUtil.get());
        return ResponseEntity.success();
    }

    @ApiOperation(
            value = "用户忘记密码-校验用户名",
            notes = "该接口提供了用户忘记密码-校验用户名的功能",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @LoginIgnore
    @PostMapping("/username/check")
    public ResponseEntity checkUsername(@Validated @RequestBody CheckUsernamePO checkUsernamePO){
        CheckUsernameContext checkUsernameContext = userConverter.checkUsernamePO2CheckUsernameContext(checkUsernamePO);
        String question = iUserService.checkUsername(checkUsernameContext);
        return ResponseEntity.success(question);
    }

    @ApiOperation(
            value = "用户忘记密码-校验用户密保",
            notes = "该接口提供了用户忘记密码-校验用户密保答案的功能",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @LoginIgnore
    @PostMapping("/answer/check")
    public ResponseEntity checkAnswer(@Validated @RequestBody CheckAnswerPO checkAnswerPO){
        CheckAnswerContext checkAnswerContext = userConverter.checkAnswerPO2CheckAnswerContext(checkAnswerPO);
        String temporaryToken = iUserService.checkAnswer(checkAnswerContext);
        return ResponseEntity.success(temporaryToken);
    }

    @ApiOperation(
            value = "用户忘记密码-重置用户密码",
            notes = "该接口提供了用户忘记密码-重置用户密码的功能",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @PostMapping("/password/reset")
    public ResponseEntity resetPassword(@Validated @RequestBody ResetPasswordPO resetPasswordPO){
        ResetPasswordContext resetPasswordContext = userConverter.resetPasswordPO2ResetPasswordContext(resetPasswordPO);
        iUserService.resetPassword(resetPasswordContext);
        return ResponseEntity.success();
    }

    @ApiOperation(
            value = "用户在线修改密码",
            notes = "该接口提供了用户修改密码的功能",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @PostMapping("/password/change")
    public ResponseEntity changePassword(@Validated @RequestBody ChangePasswordPO changePasswordPO){
        ChangePasswordContext changePasswordContext = userConverter.changePasswordPO2ChangePasswordContext(changePasswordPO);
        changePasswordContext.setUserId(IdUtil.get());
        iUserService.changePassword(changePasswordContext);
        return ResponseEntity.success();
    }

    @ApiOperation(
            value = "查询登录用户的基本信息",
            notes = "该接口提供了查询登录用户的基本信息的功能",
            consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @GetMapping("/user")
    public ResponseEntity<UserInfoVO> info(){
        UserInfoVO userInfoVO = iUserService.info(UserIdUtil.get());
        return ResponseEntity.success(userInfoVO);
    }
}
